import { Controller, Get, Post, Put, Delete, Param, Body, UseGuards } from '@nestjs/common';
import { DataService } from './data.service';
import { DataTemplate } from './data-template.entity';
import { DataRecord } from './data-record.entity';
import { JwtHttpGuard } from '../auth/jwt-http.guard';

@Controller('data')
@UseGuards(JwtHttpGuard)
export class DataController {
  constructor(private readonly dataService: DataService) {}

  // 模板相关接口
  @Get('templates')
  async findAllTemplates(): Promise<DataTemplate[]> {
    return await this.dataService.findAllTemplates();
  }

  @Get('templates/:id')
  async findOneTemplate(@Param('id') id: string): Promise<DataTemplate | null> {
    return await this.dataService.findOneTemplate(+id);
  }

  @Post('templates')
  async createTemplate(@Body() templateData: Partial<DataTemplate>): Promise<DataTemplate> {
    return await this.dataService.createTemplate(templateData);
  }

  @Put('templates/:id')
  async updateTemplate(
    @Param('id') id: string,
    @Body() templateData: Partial<DataTemplate>,
  ): Promise<DataTemplate | null> {
    return await this.dataService.updateTemplate(+id, templateData);
  }

  @Delete('templates/:id')
  async removeTemplate(@Param('id') id: string): Promise<void> {
    return await this.dataService.removeTemplate(+id);
  }

  // 数据记录相关接口
  @Get('records')
  async findAllRecords(): Promise<DataRecord[]> {
    return await this.dataService.findAllRecords();
  }

  @Get('records/template/:templateId')
  async findRecordsByTemplate(@Param('templateId') templateId: string): Promise<DataRecord[]> {
    return await this.dataService.findRecordsByTemplate(+templateId);
  }

  @Get('records/:id')
  async findOneRecord(@Param('id') id: string): Promise<DataRecord | null> {
    return await this.dataService.findOneRecord(+id);
  }

  @Post('records')
  async createRecord(@Body() recordData: Partial<DataRecord>): Promise<DataRecord> {
    return await this.dataService.createRecord(recordData);
  }

  @Put('records/:id')
  async updateRecord(
    @Param('id') id: string,
    @Body() recordData: Partial<DataRecord>,
  ): Promise<DataRecord | null> {
    return await this.dataService.updateRecord(+id, recordData);
  }

  @Delete('records/:id')
  async removeRecord(@Param('id') id: string): Promise<void> {
    return await this.dataService.removeRecord(+id);
  }
}